package com.browan.constellation.weibo;

import com.browan.constellation.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.app.ProgressDialog;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PostData extends Activity implements TextWatcher
{


	private String username;

	private String password;

	private SharedPreferences sharedPreferences;

	public static final int MSG_POST_SUCCESS = 0;
	public static final int MSG_POST_ERROR = 1;
	public static final int MSG_POST_REPEAT = 2;

	private EditText m_ContentEditText;
	private ProgressDialog m_PostDataDialog;

	private String content;

	private TextView number;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);


		if (validate())
		{
			// 用户还没有登录过

			Intent intent = new Intent(this, LoginWeiBo.class);
			intent.putExtra(Intent.EXTRA_SUBJECT, getIntent().getStringExtra(Intent.EXTRA_SUBJECT));
			intent.putExtra(Intent.EXTRA_TEXT, getIntent().getStringExtra(Intent.EXTRA_TEXT));
			startActivity(intent);
			this.finish();
		}
		else
		{
			setContentView(R.layout.sina_weibo_post);
			UserInforManager.getInstence().setUserName(username);
			UserInforManager.getInstence().setPassword(password);
			m_ContentEditText = (EditText) findViewById(R.id.content);

			String title = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);
			content = null == title ? "" : title + "\n" + getIntent().getStringExtra(Intent.EXTRA_TEXT);
			m_ContentEditText.setText(content);

			m_ContentEditText.addTextChangedListener(this);

			number = (TextView) findViewById(R.id.number);
			number.setText(String.valueOf(content.length()));

			findViewById(R.id.post).setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{

					int length = Integer.parseInt(number.getText().toString());

					if (length > 140)
					{
						Toast.makeText(PostData.this, "分享内容不能超过140个字符", Toast.LENGTH_SHORT).show();
					}
					else
					{
						m_PostDataDialog.show();
						new Thread(postRunnable).start();
					}
				}
			});
			m_PostDataDialog = new ProgressDialog(this);
			m_PostDataDialog.setTitle("更新状态");
			m_PostDataDialog.setMessage("更新中.........");
		}
	}

	public boolean validate()
	{
		sharedPreferences = getSharedPreferences(Configuration.SINA_WEIBO_FILE, Activity.MODE_PRIVATE);
		username = sharedPreferences.getString(Configuration.LOGIN_NAME, null);
		password = sharedPreferences.getString(Configuration.LOGIN_PASSWORD, null);
		return TextUtils.isEmpty(username) || TextUtils.isEmpty(password);
	}

	Runnable postRunnable = new Runnable()
	{

		@Override
		public void run()
		{
			try
			{
				String str = UpdateStatus.updateStatus(m_ContentEditText.getText().toString().trim());
				if ("400".equals(str))
				{
					handler.sendEmptyMessage(MSG_POST_REPEAT);
				}
				else
				{
					handler.sendEmptyMessage(MSG_POST_SUCCESS);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
				handler.sendEmptyMessage(MSG_POST_ERROR);
			}

		}
	};

	Handler handler = new Handler()
	{

		public void handleMessage(android.os.Message msg)
		{
			super.handleMessage(msg);

			m_PostDataDialog.dismiss();
			switch (msg.what)
			{
			case MSG_POST_ERROR:
				Toast.makeText(PostData.this, "更新失败！", Toast.LENGTH_SHORT).show();

				break;

			case MSG_POST_SUCCESS:
				 PostData.this.finish();
				break;

			case MSG_POST_REPEAT:
				Toast.makeText(PostData.this, "微博重复发送!", Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}

		};

	};

	public boolean onCreateOptionsMenu(android.view.Menu menu)
	{

		menu.add(0, Menu.FIRST, 0, "注销").setIcon(R.drawable.logout);

		return super.onCreateOptionsMenu(menu);
	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Editor editor = sharedPreferences.edit();
		editor.putString(Configuration.LOGIN_NAME, null);
		editor.putString(Configuration.LOGIN_PASSWORD, null);
		editor.commit();
		Intent intent = new Intent(this, LoginWeiBo.class);
		intent.putExtra(Intent.EXTRA_SUBJECT, getIntent().getStringExtra(Intent.EXTRA_SUBJECT));
		intent.putExtra(Intent.EXTRA_TEXT, getIntent().getStringExtra(Intent.EXTRA_TEXT));
		startActivity(intent);
		this.finish();
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void afterTextChanged(Editable s)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count)
	{
		// TODO Auto-generated method stub
		number.setText(String.valueOf(m_ContentEditText.getText().length()));
	}

}